﻿using AccessibilityTools.DB;
using LogForNetApp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace AnDaPPE
{
    public class TaskAction
    {
        #region 声明委托
        public delegate void AddActionLogHandler(string log);
        public delegate void AddActionLogHandlerJudge(int judgeNumber);
        public delegate void AddActionLogHandlerHeart(int watchDog);
        //条码委托
        public static event AddActionLogHandler AddActionLogEventHandlerOP10leftbarcode;
        public static event AddActionLogHandler AddActionLogEventHandlerOP10rightbarcode;
        public static event AddActionLogHandler AddActionLogEventHandlerOP20barcode;
        public static event AddActionLogHandler AddActionLogEventHandlerOP30barcode;
        public static event AddActionLogHandler AddActionLogEventHandlerBOXbarcode;
        public static event AddActionLogHandler AddActionLogEventHandlerProductbarcode;
        //提示委托
        public static event AddActionLogHandler AddActionLogEventHandlerOP10leftTip;
        public static event AddActionLogHandler AddActionLogEventHandlerOP10rightTip;
        public static event AddActionLogHandler AddActionLogEventHandlerOP20Tip;
        public static event AddActionLogHandler AddActionLogEventHandlerOP30Tip;
        public static event AddActionLogHandler AddActionLogEventHandlerBOXTip;
        //计数委托
        public static event AddActionLogHandlerJudge AddActionLogEventHandlerOP10LeftJudge;
        public static event AddActionLogHandlerJudge AddActionLogEventHandlerOP10RightJudge;
        public static event AddActionLogHandlerJudge AddActionLogEventHandlerOP20Judge;
        public static event AddActionLogHandlerJudge AddActionLogEventHandlerOP30Judge;
        //心跳委托
        public static event AddActionLogHandlerHeart AddActionLogEventHeartOP10;
        public static event AddActionLogHandlerHeart AddActionLogEventHeartOP20;
        public static event AddActionLogHandlerHeart AddActionLogEventHeartOP30;
        #endregion

        #region 声明变量
        ProductionDetailsInfoLogic productionDetailsInfoLogic = new ProductionDetailsInfoLogic();//生产数据库集合通过add方法写入数据库
        ProductionDetailsInfoService productionDetailsInfo = new ProductionDetailsInfoService();//生产数据库通过这个类可以拿到数据库方法
        ProductionDetailsInfo Instance = new ProductionDetailsInfo();

        PlcToPc _plcToPc;
        PcToPlc _pcToPlc;

        short _lastTcOP10left;//复位信号
        short _lastTcOP10right;
        short _lastTcOP20;
        short _lastTcOP30;
        short _lastTcBox;

        bool _tick10;//心跳op10....
        bool _tick20;
        bool _tick30;
        #endregion

        #region 开启心跳和扫码业务线程
        /// <summary>
        /// 心跳信号开启，扫码信号开启，装箱信号开启
        /// </summary>
        public void StartAction(PlcController plc, int plcNumber)
        {
            this._plcToPc = new PlcToPc(plc);
            this._pcToPlc = new PcToPlc(plc);

            Thread.Sleep(1000);//需要先等数据库先连接成功，有可能会报错

            Task.Run(async () =>
            {
                await HeartbeatAction(plc, plcNumber); //心跳
            });

            //Task.Run(() =>
            // {
            //     ScanEvent(plc, plcNumber);//扫码业务不能使用异步方法

            // });
            Task.Factory.StartNew(() =>
            {
                ScanEvent(plc, plcNumber);//扫码业务不能使用异步方法
            }
            , TaskCreationOptions.LongRunning);

        }
        #endregion


        #region 开启扫码业务
        /// <summary>
        /// 循环接收每个工站的PLC扫码业务，扫码通道plcNumber号码分别为1,2 为op10,3为op20,4为op30
        /// </summary>
        /// <returns></returns>

        private void ScanEvent(PlcController plc, int _channelNum)
        {
            while (plc.IsConnected)
            {

                #region OP10左工位工作
                int uploadStartOP10left = _plcToPc.UploadStartOP10Left;//循环接收到扫码请求

                if (uploadStartOP10left == 1 && uploadStartOP10left != _lastTcOP10left)
                {

                    //清除扫码状态
                    _pcToPlc.UploadScanDoneOP10Left = 0;
                    //清除允许加工状态
                    _pcToPlc.AllowOP10Left = 0;
                    //清除上传数据完成信号
                    _pcToPlc.UploadDoneWorkOP10Left = 0;

                    LogHelper.WriteInfo("业务,已接收到plcOP10扫码信号");//需要给plc读取条码清零信号吗

                    //读取条码
                    string sn = "";
                    switch (_channelNum)
                    {
                        case 1:
                            sn = _plcToPc.Sn_ChannelOP10A(); //DB1000, 起始地址280,读取个数 40
                            LogHelper.WriteInfo($"业务,已接收到plcOP10左工位扫码{sn}");
                            //条码不能为空给plc状态1或者2
                            _pcToPlc.UploadScanDoneOP10Left = sn != null ? 1 : 2;

                            //接收到产品码并且查询数据库是否存在条码信息
                            if (!string.IsNullOrEmpty(sn))//条码不为空 ,注意sn需要用单引号语法表示要不会报错
                            {
                                try
                                {
                                    //截取二维码0EN和0ENP等 截取到第四位
                                    string staionInfo = sn.Substring(0, 4);

                                    AddActionLogEventHandlerOP10leftbarcode($"{sn}");
                                    Instance = productionDetailsInfoLogic.GetInstance(sn);

                                    if (Instance != null && staionInfo == VariableGlobal.Transfer)//说明有加工记录直接加工
                                    {


                                        if (Instance.Op10LeftResult == 0 || Instance.Op10LeftResult == 1)
                                        {

                                            AddActionLogEventHandlerOP10leftTip("可以加工");
                                            _pcToPlc.AllowOP10Left = 1;//允许加工
                                            // instance.TestResult = testResult == true ? "OK" : "NG";
                                            CompletionsignalOP10Left(plc, sn, "返工");
                                        }


                                    }
                                    else if (staionInfo == VariableGlobal.Transfer)

                                    {
                                        AddActionLogEventHandlerOP10leftTip("可以加工");
                                        _pcToPlc.AllowOP10Left = 1;
                                        CompletionsignalOP10Left(plc, sn, "加工");

                                    }
                                    else
                                    {
                                        AddActionLogEventHandlerOP10leftTip("产品班次错误");
                                        _pcToPlc.AllowOP10Left = 2; //不允许加工

                                    }
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex.Message);
                                    throw;
                                }

                            }

                            else
                            {
                                AddActionLogEventHandlerOP10leftTip("没有读到条码内容");
                                _pcToPlc.AllowOP10Left = 2;
                            }
                            break;

                    }
                    //这里为1防止下次循环进入
                    _lastTcOP10left = (short)uploadStartOP10left;

                }
                //当plc将上传信号改变为零时候复位_lastTcOP10left，等待下次为1时候进入
                else if (uploadStartOP10left == 0)
                {
                    _lastTcOP10left = 0;
                }

                #endregion

                #region OP10右工位工作
                int uploadStartOP10right = _plcToPc.UploadStartOP10Right;//接收信号循环读取"DB1000.DBW11"左工位信号
                if (uploadStartOP10right == 1 && uploadStartOP10right != _lastTcOP10right)
                {

                    //清除扫码状态
                    _pcToPlc.UploadScanDoneOP10Right = 0;
                    //清除允许加工状态
                    _pcToPlc.AllowOP10Right = 0;
                    //清除上传数据完成信号
                    _pcToPlc.UploadDoneWorkOP10Right = 0;

                    LogHelper.WriteInfo("业务,已接收到plcOP10右工位扫码信号");

                    //读取条码
                    string sn = "";
                    switch (_channelNum)
                    {

                        case 2:
                            sn = _plcToPc.Sn_ChannelOP10B();//DB1000, 起始地址322,读取个数 40
                            LogHelper.WriteInfo($"业务,已接收到plcOP10右工站扫码{sn}");
                            //接收到产品码并且查询数据库是否存在条码信息
                            //条码不能为空给plc状态1或者2
                            _pcToPlc.UploadScanDoneOP10Right = sn != null ? 1 : 2;

                            if (!string.IsNullOrEmpty(sn))//条码不为空 一旦为空则跳工站
                            {
                                try
                                {
                                    //截取二维码0EN和0ENP等 截取到第四位
                                    string staionInfo = sn.Substring(0, 4);

                                    //传递条码
                                    AddActionLogEventHandlerOP10rightbarcode($"{sn}");
                                    //查询有关条码中的所有站的结果值
                                    // instance = productionDetailsInfo.GetList($"SELECT op10_left_result,op10_right_result,op20_result,op30_result FROM cks2013.production_details_info where product_sn='{sn}'");
                                    //插入新的记录
                                    //productionDetailsInfo.GetList($"insert into  cks2013.production_details_info(product_sn) values('{sn}');");
                                    Instance = productionDetailsInfoLogic.GetInstance(sn);
                                    if (Instance != null)
                                    {
                                        if (Instance.Op10LeftResult == 1 && staionInfo == VariableGlobal.Transfer)//先查上一工站判断是否跳工站
                                        {

                                            if (Instance.Op10RightResult == 0 || Instance.Op10RightResult == 1)
                                            {

                                                AddActionLogEventHandlerOP10rightTip("可以加工");
                                                _pcToPlc.AllowOP10Right = 1;
                                                CompletionsignalOP10Right(plc, sn, "加工");

                                            }

                                            else
                                            {
                                                AddActionLogEventHandlerOP10rightTip("不可以加工");
                                                _pcToPlc.AllowOP10Right = 2;
                                            }



                                        }
                                        else if (staionInfo == VariableGlobal.Transfer)
                                        {
                                            AddActionLogEventHandlerOP10rightTip("跳工站不可以加工");
                                            //不允许加工给plc允许加工信号
                                            _pcToPlc.AllowOP10Right = 2;

                                        }
                                        else
                                        {
                                            AddActionLogEventHandlerOP10rightTip("产品班次错误");
                                            //不允许加工给plc允许加工信号
                                            _pcToPlc.AllowOP10Right = 2;
                                        }
                                    }
                                    else
                                    {
                                        AddActionLogEventHandlerOP10rightTip("没有查询到生产信息");
                                        //不允许加工给plc允许加工信号
                                        _pcToPlc.AllowOP10Right = 2;
                                    }

                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex.Message);
                                    throw;
                                }

                            }
                            //如果为空证明没有读到条码
                            else
                            {
                                AddActionLogEventHandlerOP10rightTip("没有读到条码内容");
                                _pcToPlc.AllowOP10Right = 2;
                            }
                            break;

                    }


                    _lastTcOP10right = (short)uploadStartOP10right;
                }
                else if (uploadStartOP10right == 0)
                {
                    _lastTcOP10right = 0;
                }

                #endregion

                #region OP20工作
                int uploadStartOP20 = _plcToPc.UploadStartOP20;//接收信号循环读取"DB1000.DBW10"
                if (uploadStartOP20 == 1 && uploadStartOP20 != _lastTcOP20)
                {
                    //清除扫码状态
                    _pcToPlc.UploadScanDoneOP20 = 0;
                    //清除允许加工状态
                    _pcToPlc.AllowOP20 = 0;
                    //清除上传数据完成信号
                    _pcToPlc.UploadWorkDoneOP20 = 0;

                    LogHelper.WriteInfo("业务,已接收到plcOP20扫码信号");

                    //读取条码
                    string sn = "";
                    switch (_channelNum)
                    {
                        case 3:
                            sn = _plcToPc.Sn_ChannelOP20(); //DB1000, 起始地址280,读取个数 40
                            LogHelper.WriteInfo($"业务,已接收到plcOP20左工站扫码{sn}");

                            //条码不能为空给plc状态1或者2
                            _pcToPlc.UploadScanDoneOP20 = sn != null ? 1 : 2;

                            if (!string.IsNullOrEmpty(sn))//条码不为空 一旦为空则跳工站
                            {
                                try
                                {
                                    //截取二维码0EN和0ENP等 截取到第四位
                                    string staionInfo = sn?.Substring(0, 4);
                                    //传递条码
                                    AddActionLogEventHandlerOP20barcode($"{sn}");
                                    //查询有关条码中的所有站的结果值
                                    // instance = productionDetailsInfo.GetList($"SELECT op10_left_result,op10_right_result,op20_result,op30_result FROM cks2013.production_details_info where product_sn='{sn}'");
                                    //插入新的记录
                                    //productionDetailsInfo.GetList($"insert into  cks2013.production_details_info(product_sn) values('{sn}');");
                                    Instance = productionDetailsInfoLogic.GetInstance(sn);
                                    if (Instance != null)
                                    {
                                        if (Instance.Op10RightResult == 1 && staionInfo == VariableGlobal.Transfer)//先查上一工站判断是否跳工站
                                        {


                                            if (Instance.Op20Result == 0 || Instance.Op20Result == 1)
                                            {

                                                AddActionLogEventHandlerOP20Tip("可以加工");
                                                _pcToPlc.AllowOP20 = 1;
                                                CompletionsignalOP20(plc, sn, "加工");

                                            }

                                            else
                                            {
                                                AddActionLogEventHandlerOP20Tip("不可以加工");
                                                _pcToPlc.AllowOP20 = 2;
                                            }



                                        }
                                        else if (staionInfo == VariableGlobal.Transfer)
                                        {
                                            AddActionLogEventHandlerOP20Tip("跳工站不可以加工");
                                            //不允许加工给plc允许加工信号
                                            _pcToPlc.AllowOP20 = 2;

                                        }
                                        else
                                        {
                                            AddActionLogEventHandlerOP20Tip("产品班次错误");
                                            //不允许加工给plc允许加工信号
                                            _pcToPlc.AllowOP20 = 2;
                                        }
                                    }
                                    else
                                    {
                                        AddActionLogEventHandlerOP20Tip("没有查询到生产信息");
                                        //不允许加工给plc允许加工信号
                                        _pcToPlc.AllowOP20 = 2;
                                    }

                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex.Message);
                                    throw;
                                }

                            }
                            //如果为空证明没有读到条码
                            else
                            {
                                AddActionLogEventHandlerOP20Tip("没有读到条码内容");
                                _pcToPlc.AllowOP20 = 2;
                            }
                            break;


                    }
                    _lastTcOP20 = (short)uploadStartOP20;
                }
                else if (uploadStartOP20 == 0)

                {
                    _lastTcOP20 = 0;
                }

                #endregion

                #region OP30工作
                int uploadStartOP30 = _plcToPc.UploadStartOP30;//接收信号循环读取"DB1000.DBW10"
                if (uploadStartOP30 == 1 && uploadStartOP30 != _lastTcOP30)
                {
                    //清除扫码状态信号是否扫上码初始为0 成功1 失败2
                    _pcToPlc.UploadScanDoneOP30 = 0;
                    //清除允许加工状态
                    _pcToPlc.AllowOP30 = 0;
                    //清除数据上传完成信号
                    _pcToPlc.UploadWorkDoneOP30 = 0;

                    LogHelper.WriteInfo("业务,已接收到plcOP20扫码信号");

                    //读取条码
                    string sn = "";
                    switch (_channelNum)
                    {
                        case 4:
                            sn = _plcToPc.Sn_ChannelOP30(); //DB1000, 起始地址280,读取个数 40
                            LogHelper.WriteInfo($"业务,已接收到plcOP30左工站扫码{sn}");

                            //条码不能为空给plc状态1或者2
                            _pcToPlc.UploadScanDoneOP30 = sn != null ? 1 : 2;

                            if (!string.IsNullOrEmpty(sn))//条码不为空 
                            {
                                try
                                {
                                    //截取二维码0EN和0ENP等 截取到第四位
                                    string staionInfo = sn?.Substring(0, 4);
                                    //传递条码
                                    AddActionLogEventHandlerOP30barcode($"{sn}");
                                    //查询有关条码中的所有站的结果值
                                    // Instance = productionDetailsInfo.GetList($"SELECT op10_left_result,op10_right_result,op20_result,op30_result FROM cks2013.production_details_info where product_sn='{sn}'");
                                    Instance = productionDetailsInfoLogic.GetInstance(sn);
                                    if (Instance != null)
                                    {
                                        if (Instance.Op20Result == 1 && staionInfo == VariableGlobal.Transfer)
                                        {

                                            if (Instance.Op30Result == 0 || Instance.Op30Result == 1)
                                            {

                                                AddActionLogEventHandlerOP30Tip("可以加工");
                                                _pcToPlc.AllowOP30 = 1;
                                                CompletionsignalOP30(plc, sn, "加工");

                                            }

                                            else
                                            {
                                                AddActionLogEventHandlerOP30Tip("不可以加工");
                                                _pcToPlc.AllowOP30 = 2;
                                            }



                                        }
                                        else if (staionInfo == VariableGlobal.Transfer)

                                        {
                                            AddActionLogEventHandlerOP30Tip("跳工站不可以加工");
                                            //不允许加工给plc允许加工信号
                                            _pcToPlc.AllowOP30 = 2;

                                        }
                                        else
                                        {
                                            AddActionLogEventHandlerOP30Tip("产品班次错误");
                                            //不允许加工给plc允许加工信号
                                            _pcToPlc.AllowOP30 = 2;
                                        }
                                    }
                                    else
                                    {
                                        AddActionLogEventHandlerOP30Tip("没有查询到生产信息");
                                        //不允许加工给plc允许加工信号
                                        _pcToPlc.AllowOP30 = 2;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex.Message);
                                    //throw;
                                }

                            }
                            //如果为空证明没有读到条码
                            else
                            {
                                AddActionLogEventHandlerOP30Tip("没有读到条码内容");
                                _pcToPlc.AllowOP30 = 2;
                            }
                            break;


                    }
                    _lastTcOP30 = (short)uploadStartOP30;
                }
                else if (uploadStartOP30 == 0)
                {
                    _lastTcOP30 = 0;
                }
                #endregion


                Task.Delay(50);
            }
        }
        #endregion

        #region 开启心跳
        /// <summary>
        /// 执行心跳看哪个plc传入,需要判定是那个plc对象
        /// </summary>
        /// <returns></returns>
        private async Task HeartbeatAction(PlcController plc, int _channelNum)
        {

            LogHelper.WriteInfo("程序已开始发送心跳");
            _tick10 = true;
            _tick20 = true;
            _tick30 = true;
            while (plc.IsConnected)
            {
                if (_channelNum == 1)
                {
                    _pcToPlc.WatchDog10 = Convert.ToInt16(_tick10);//发送true

                    AddActionLogEventHeartOP10(_plcToPc.WatchDog10());
                    await Task.Delay(200);

                }
                _tick10 = !_tick10;//负责显示通断
                if (_channelNum == 3)
                {
                    _pcToPlc.WatchDog20 = Convert.ToInt16(_tick20);
                    AddActionLogEventHeartOP20(_plcToPc.WatchDog20());
                    await Task.Delay(200);

                }
                _tick20 = !_tick20;
                if (_channelNum == 4)
                {
                    _pcToPlc.WatchDog30 = Convert.ToInt16(_tick30);
                    AddActionLogEventHeartOP30(_plcToPc.WatchDog30());
                    await Task.Delay(200);

                }
                _tick30 = !_tick30;

            }
        }
        #endregion

        #region 完成信号监控op10左
        /// <summary>
        /// 
        /// </summary>
        /// <param name="plc">传入的plc</param>
        /// <param name="sn">当前读到的sn码</param>
        /// <param name="rework">是否返工说明</param>
        private void CompletionsignalOP10Left(PlcController plc, string sn, string rework)
        {

            while (plc.IsConnected)
            {
                //接收开始上传数据信号op10左

                int donesignOP10Left = _plcToPc.UploadDoneSignOP10Left();

                if (donesignOP10Left == 1 && rework == "加工")
                {


                    //插入新的记录
                    ProductionDetailsInfo productionDetailsInfo = new ProductionDetailsInfo()
                    {
                        ProductName = "产品1",
                        ProductSn = sn,
                        Op10LeftResult = (short)plc.ReadInt("DB1000.DBW1040"),
                        InBox = 0,
                        UpRingAirTightnessValue = (short)plc.ReadInt("DB1000.DBW1042"),
                        UpRingLeakageValue = (short)plc.ReadInt("DB1000.DBW1044"),
                        Op10LeftTesttime = DateTime.Now,
                        CreationTime = DateTime.Now,//创建当前记录时间


                    };
                    int op10Done1 = productionDetailsInfoLogic.Add(productionDetailsInfo);
                    //数据接收完成
                    _pcToPlc.UploadDoneWorkOP10Left = op10Done1 == 1 ? 1 : 2; //0初始 1完成 2 没有完成
                    //加工完成提示
                    AddActionLogEventHandlerOP10leftTip("加工完成");
                    if (productionDetailsInfo.Op10LeftResult == 1)
                    {

                        AddActionLogEventHandlerOP10LeftJudge(2);
                    }
                    else
                    {
                        AddActionLogEventHandlerOP10LeftJudge(3);
                    }
                    break;
                }
                if (donesignOP10Left == 1 && rework == "返工")
                {
                    //已经接收到加工信号
                    _pcToPlc.UploadDoneWorkOP10Left = 1;
                    //清除允许加工状态
                    _pcToPlc.AllowOP10Left = 0;

                    ProductionDetailsInfo ins = productionDetailsInfoLogic.GetInstance(sn);
                    //插入新的记录

                    ins.Op10LeftResult = (short)plc.ReadInt("DB1000.DBW1040");
                    ins.UpRingAirTightnessValue = (short)plc.ReadInt("DB1000.DBW1042");
                    ins.UpRingLeakageValue = (short)plc.ReadInt("DB1000.DBW1044");
                    ins.Op10LeftTesttime = DateTime.Now;
                    int op10Done2 = productionDetailsInfoLogic.UpDate(ins);
                    //数据接收完成
                    _pcToPlc.UploadDoneWorkOP10Left = op10Done2 == 1 ? 1 : 2; //0初始 1完成 2 没有完成
                    //加工完成提示
                    AddActionLogEventHandlerOP10leftTip("加工完成");
                    if (ins.Op10LeftResult == 1)
                    {
                        AddActionLogEventHandlerOP10LeftJudge(2);
                    }
                    else
                    {
                        AddActionLogEventHandlerOP10LeftJudge(3);
                    }
                    break;
                }
                Task.Delay(100);
            }
        }
        #endregion

        #region 完成信号监控op10右
        /// <summary>
        /// 
        /// </summary>
        /// <param name="plc">传入的plc</param>
        /// <param name="sn">当前读到的sn码</param>
        /// <param name="rework">是否返工说明</param>
        private void CompletionsignalOP10Right(PlcController plc, string sn, string rework)
        {

            while (plc.IsConnected)
            {
                ////接收信号加工完成信号op10右

                // int donesignOP10Right = plc.ReadInt("DB1000.DBW1046");
                int donesignOP10Right = _plcToPc.UploadDoneSignOP10Right();

                if (donesignOP10Right == 1 && rework == "加工")
                {

                    ProductionDetailsInfo ins = productionDetailsInfoLogic.GetInstance(sn);

                    ins.Op10RightResult = (short)plc.ReadInt("DB1000.DBW1048");
                    ins.DownRingAirTightnessValue = (short)plc.ReadInt("DB1000.DBW1050");
                    ins.DownRingLeakageValue = (short)plc.ReadInt("DB1000.DBW1052");
                    ins.Op10RightTesttime = DateTime.Now;
                    int op20Done = productionDetailsInfoLogic.UpDate(ins);
                    //数据上传完成信号
                    _pcToPlc.UploadDoneWorkOP10Right = op20Done == 1 ? 1 : 2; //0初始 1完成 2 没有完成
                    //加工完成提示
                    AddActionLogEventHandlerOP10rightTip("加工完成");
                    if (ins.Op10RightResult == 1)
                    {
                        AddActionLogEventHandlerOP10RightJudge(2);
                    }
                    else
                    {
                        AddActionLogEventHandlerOP10RightJudge(3);
                    }
                    break;
                }

                Task.Delay(100);
            }
        }
        #endregion

        #region 完成信号监控op20
        /// <summary>
        /// 
        /// </summary>
        /// <param name="plc">传入的plc</param>
        /// <param name="sn">当前读到的sn码</param>
        /// <param name="rework">是否返工说明</param>
        private void CompletionsignalOP20(PlcController plc, string sn, string rework)
        {

            while (plc.IsConnected)
            {
                ////接收信号加工完成信号op20

                //int donesignOP10Right = plc.ReadInt("DB1000.DBW1054");
                int donesignOP10Right = _plcToPc.UploadDoneSignOP20();

                if (donesignOP10Right == 1 && rework == "加工")
                {



                    ProductionDetailsInfo ins = productionDetailsInfoLogic.GetInstance(sn);

                    ins.Op20Result = (short)plc.ReadInt("DB1000.DBW1056");
                    ins.Flowmeter1MeasurementValue = (short)plc.ReadInt("DB1000.DBW1058");
                    ins.FlowMeter1LeakageThreshold = (short)plc.ReadInt("DB1000.DBW1060");
                    ins.Flowmeter2MeasurementValue = (short)plc.ReadInt("DB1000.DBW1062");
                    ins.FlowMeter2LeakageThreshold = (short)plc.ReadInt("DB1000.DBW1064");
                    ins.Flowmeter3MeasurementValue = (short)plc.ReadInt("DB1000.DBW1066");
                    ins.FlowMeter3LeakageThreshold = (short)plc.ReadInt("DB1000.DBW1068");
                    ins.Op20Testtime = DateTime.Now;
                    int op20Done = productionDetailsInfoLogic.UpDate(ins);
                    //数据上传完成信号
                    _pcToPlc.UploadWorkDoneOP20 = op20Done == 1 ? 1 : 2; //0初始 1完成 2 没有完成
                    //加工完成提示
                    AddActionLogEventHandlerOP20Tip("加工完成");

                    if (ins.Op20Result == 1)
                    {
                        AddActionLogEventHandlerOP20Judge(2);
                    }
                    else
                    {
                        AddActionLogEventHandlerOP20Judge(3);
                    }
                    break;
                }

                Task.Delay(100);
            }
        }
        #endregion

        #region 完成信号监控op30
        /// <summary>
        /// 
        /// </summary>
        /// <param name="plc">传入的plc</param>
        /// <param name="sn">当前读到的sn码</param>
        /// <param name="rework">是否返工说明</param>
        private void CompletionsignalOP30(PlcController plc, string sn, string rework)
        {

            while (plc.IsConnected)
            {

                //读取op30加工完成信号op30
                // int donesignOP30 = plc.ReadInt("DB1000.DBW1070");
                int donesignOP30 = _plcToPc.UploadDoneSignOP30();

                if (donesignOP30 == 1 && rework == "加工")
                {

                    ProductionDetailsInfo ins = productionDetailsInfoLogic.GetInstance(sn);
                    ins.Op30Result = (short)plc.ReadInt("DB1000.DBW1072");
                    //目前不清楚装配机信号需要和plc对
                    ins.FreeValue1 = (short)plc.ReadInt("DB1000.DBW1074");
                    ins.FreeValue2 = (short)plc.ReadInt("DB1000.DBW1076");
                    ins.Op30Testtime = DateTime.Now;
                    int op30Done = productionDetailsInfoLogic.UpDate(ins);

                    //数据上传完成信号
                    _pcToPlc.UploadWorkDoneOP30 = op30Done == 1 ? 1 : 2; //0初始 1完成 2 没有完成
                                                                         //加工完成提示
                    AddActionLogEventHandlerOP30Tip("加工完成");
                    if (ins.Op30Result == 1)
                    {
                        AddActionLogEventHandlerOP30Judge(2);
                    }
                    else
                    {
                        AddActionLogEventHandlerOP30Judge(3);
                    }
                    break;
                }

                Task.Delay(100);
            }
        }
        #endregion

        #region 开启监控装箱扫码信号
        /// <summary>
        /// 
        /// </summary>
        /// <param name="plc">传入的plc</param>
        /// <param name="sn">当前读到的sn码</param>
        /// <param name="rework">是否返工说明</param>
        private void CompletionsignalBox(PlcController plc)
        {

            while (plc.IsConnected)
            {
                #region OP30工作
                int uploadStartBox = _plcToPc.UploadStartBox;//接收信号加工完成信号

                if (uploadStartBox == 1 && uploadStartBox != _lastTcBox)
                {
                    LogHelper.WriteInfo("业务,已接收到plc装箱扫码信号");
                    //读取条码
                    string sn_box = "";
                    string sn_product = "";
                    sn_box = _plcToPc.Sn_ChannelBox(); //读取装箱码
                    sn_product = _plcToPc.Sn_ChannelProductSN();//读取产品码
                    LogHelper.WriteInfo($"业务,已接收到plc扫码{sn_box}");
                    LogHelper.WriteInfo($"业务,已接收到plc扫码{sn_product}");

                    if (!string.IsNullOrEmpty(sn_box) && !string.IsNullOrEmpty(sn_product))//条码不为空 
                    {
                        try
                        {
                            //传递条码
                            AddActionLogEventHandlerBOXbarcode($"{sn_box}");
                            AddActionLogEventHandlerProductbarcode($"{sn_product}");
                            //查询有关条码中的所有站的结果值
                            // Instance = productionDetailsInfo.GetList($"SELECT op10_left_result,op10_right_result,op20_result,op30_result FROM cks2013.production_details_info where product_sn='{sn}'");
                            Instance = productionDetailsInfoLogic.GetInstance(sn_product);
                            if (Instance != null)
                            {

                                if (Instance.Op10LeftResult == 1 && Instance.Op10RightResult == 1 && Instance.Op20Result == 1 && Instance.Op30Result == 1)//判断每个工站都为ok才能装箱
                                {
                                    AddActionLogEventHandlerBOXTip("可以装箱");


                                }
                                else
                                {
                                    //提示那个工站错误不能装箱，需要返工
                                    AddActionLogEventHandlerBOXTip("不可以装箱");

                                }
                            }
                            else
                            {
                                AddActionLogEventHandlerOP30Tip("跳工站不可以加工");
                                //不允许加工给plc允许加工信号
                                _pcToPlc.AllowOP30 = 101;
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                            throw;
                        }

                    }


                    _lastTcBox = (short)uploadStartBox;
                }
                else if (uploadStartBox == 0)
                {
                    _lastTcBox = 0;
                }
                #endregion
            }
        }
        #endregion

    }
}
